package com.ironman.h6b1gw.jep;

import jep.Interpreter;
import jep.MainInterpreter;
import jep.SharedInterpreter;
import org.noear.snack.ONode;

/**
 * java环境中，执行python代码
 */
public class JepDemo {

    public static void main(String[] args) {
        MainInterpreter.setJepLibraryPath("C:\\Users\\89754\\AppData\\Roaming\\Python\\Python312\\site-packages\\jep\\jep.dll");
        try (Interpreter interp = new SharedInterpreter()) {
            interp.set("name", "tan90");
            interp.set("age", 35);
            interp.exec("import json\nimport sys\n" +
                    "\n" +
                    "def check(name):\n" +
                    "    return {\n" +
                    "        \"name\": name,\n" +
                    "        \"age\": age,\n" +
                    "        \"path\": sys.path,\n" +
                    "    }\n");
            interp.exec("res = check(name)");
            Object res = interp.getValue("res");
            ONode node = ONode.loadObj(res);
            System.out.println(node.toJson());
        }
    }
}
